package ita.class1.group2.supermarket.administrator.controller;

import ita.class1.group2.supermarket.common.util.RespMessageFactory;
import ita.class1.group2.supermarket.common.annotation.Autowired;
import ita.class1.group2.supermarket.common.annotation.Component;
import ita.class1.group2.supermarket.common.annotation.RequestMapping;
import ita.class1.group2.supermarket.common.auth.SecurityUtils;
import ita.class1.group2.supermarket.common.auth.UsernamePasswordToken;
import ita.class1.group2.supermarket.common.auth.exception.AccountException;
import ita.class1.group2.supermarket.common.pojo.ResponseMessage;
import ita.class1.group2.supermarket.common.util.Md5Utils;

import java.util.Map;

/**
 * @ Author     ：LILA3
 * @ Date       ：Created in 5:05 PM 7/21/2018
 */
@Component
public class UserController {

    @Autowired
    private SecurityUtils securityUtils;


    @RequestMapping("/login")
    public ResponseMessage login(Map<String,String> map) {
        String usernames = map.get("username");
        String password = map.get("password");
        try {
            securityUtils.getSubject().login(new UsernamePasswordToken(usernames, Md5Utils.md5Password(password)));
        } catch (AccountException e) {
            return RespMessageFactory.createSuccessMessage(false);
        }
        return RespMessageFactory.createSuccessMessage(securityUtils.getSubject().isLogged());
    }


    @RequestMapping("/admin/logout")
    public ResponseMessage login() {
        securityUtils.getSubject().logout();
        return RespMessageFactory.createSuccessMessage(false);
    }




}
